using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;


public partial class StoredProcedures
{
    [Microsoft.SqlServer.Server.SqlProcedure]
    public static void SelectManyLookup(SqlString TableName, SqlString ValueColumnName, SqlString DisplayColumnName)
    {
        using (SqlConnection connection = new SqlConnection("context connection=true"))
        {
            try
            {
                connection.Open();

                string selectManyLookupCommandText = string.Format(
                    " SELECT {1} AS Value, {2} AS Display FROM {0} ",
                    TableName, ValueColumnName, DisplayColumnName);

                SqlCommand selectManyLookupCommand = new SqlCommand(selectManyLookupCommandText, connection);

                SqlContext.Pipe.ExecuteAndSend(selectManyLookupCommand);
            }
            catch (System.Exception exception)
            {
                SqlContext.Pipe.Send("An exception occured");
                SqlContext.Pipe.Send("Message: " + exception.Message);
                SqlContext.Pipe.Send("Source: " + exception.Source);
                SqlContext.Pipe.Send("Stack: " + exception.StackTrace);
                SqlContext.Pipe.Send("Target: " + exception.TargetSite);
            }
            finally
            {
                connection.Close();
            }
        }
    }
};
